/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package checkers.businessLogic;

import checkers.businessLogic.entity.User;
import checkers.database.MySQLUserOperations;
import checkers.exceptions.EmailNotUniqueException;
import java.sql.Date;


import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author artinshahverdian
 */
public class UserManager {

    private static UserManager instance = null;
    MySQLUserOperations mySQLUserOperations;

    private UserManager() throws Exception {

        mySQLUserOperations = MySQLUserOperations.GetInstance();
    }

    public static UserManager getInstance() throws Exception {
        if (instance == null)
            instance = new UserManager();
        return instance;
    }

    public User login(String email, String password) throws SQLException {

        User user = mySQLUserOperations.getUser(email, password);

        if (user != null) {
            Integer userId = mySQLUserOperations.getId(email);
            Timestamp loginDate = new Timestamp(System.currentTimeMillis());
            mySQLUserOperations.updateLastLogin(""+userId, loginDate);
        }

        return user;





    }

    public void register(String email, String password, String firstName,
            String lastName, Date birthDate, String securityQuestion,
            String securityAnswer) throws EmailNotUniqueException, SQLException {

//        User user = new User(email, password, firstName, lastName, birthDate,
//                securityQuestion, securityAnswer, 1,
//                null,
//                new Timestamp(Calendar.getInstance().getTimeInMillis()));


        User user = new User(email, password, firstName, lastName, birthDate, securityQuestion, securityAnswer, 1, null, new Timestamp(System.currentTimeMillis()));
        mySQLUserOperations.registerUser(user);

    }

    public String getSecurityQuestion(String email) throws SQLException {

        return mySQLUserOperations.getSecurityQuestion(email);




    }

    public String recoverPassword(String email, String securityAnswer) throws SQLException {

        String dbSecurityAnswer = mySQLUserOperations.getSecurityAnswer(email);
        if (dbSecurityAnswer.toLowerCase().compareTo(securityAnswer.toLowerCase()) == 0)
            return mySQLUserOperations.getPassword(email);
        else
            return null;

    }
    
    public void updateRating(String userId, int rating) throws SQLException{
        
        mySQLUserOperations.updateRating(userId, rating);
        
    }

    public String getNameFromUserId(String userId) throws SQLException {

        return mySQLUserOperations.getName(userId);

    }

    public String getEmailFromUserId(String userId) throws SQLException {

        return mySQLUserOperations.getEmail(userId);

    }

    public int getRating(String userId) throws SQLException {
        return mySQLUserOperations.getRating(userId);
    }

    public LinkedList<User> getHighRankings() throws SQLException {
        return mySQLUserOperations.getHighRanking();
    }

    public boolean editProfile(String email, String password, String firstName, String lastName, Date birthdate,
            String securityQuestion, String securityAnswer, String userId) throws SQLException {
        
            mySQLUserOperations.updateEmail(userId, email);
            mySQLUserOperations.updatePassword(userId, password);
            mySQLUserOperations.updateFirstName(userId, firstName);
            mySQLUserOperations.updateLastName(userId, lastName);
            mySQLUserOperations.updateBirthDate(userId, birthdate);
            mySQLUserOperations.updateSecurityQuestion(userId, securityQuestion);
            mySQLUserOperations.updateSecurityAnswer(userId, securityAnswer);
            return true;
       
    }

    public User getUserById(String userId) throws SQLException {
        User user = mySQLUserOperations.getUserById(userId);
        return user;



    }
}